<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 15] URL</TITLE>
<META NAME="author" CONTENT="Mark Grand and Jonathan Knudsen">
<META NAME="date" CONTENT="Fri Aug  8 17:23:04 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java Fundamental Classes Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Fundamental Classes Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch15_19.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 15<br>The java.net Package</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch15_21.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=refnamediv>
<H1>URL</H1>

<H2>Name</H2>

URL

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.129">Synopsis</A></h2>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Class Name:<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>java.net.URL</tt>

<p>
<DT CLASS=varlistentry>Superclass:<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>java.lang.Object</tt>

<p>
<DT CLASS=varlistentry>Immediate Subclasses:<br>
<DD>

<P CLASS=para>
None

<p>
<DT CLASS=varlistentry>Interfaces Implemented:<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>java.io.Serializable</tt>

<p>
<DT CLASS=varlistentry>Availability:<br>
<DD>

<P CLASS=para>
JDK 1.0 or later </DL>
</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.130">Description</A></h2>

<P CLASS=para>
The <tt CLASS=literal>URL</tt> class represents a 
Uniform Resource Locator, or URL. The class provides methods for retrieving 
the various parts of a URL and also access to the resource itself. 

<P CLASS=para>
An absolute URL consists of a protocol, a hostname, a port number, a filename, 
and an optional reference, or anchor. For example, consider the following 
URL: 

<DIV CLASS=screen>
<P>
<PRE>
http://www.woolf.net:81/books/Orlando/chapter4.html#p6
</PRE>
</DIV>

<P CLASS=para>
This URL consists of the following parts: 

<DIV CLASS=informaltable>
<P>
<TABLE CLASS=INFORMALTABLE>
<TR CLASS=row>
<TH ALIGN="left">Part</TH>
<TH ALIGN="left">Value</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
Protocol</TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>http</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
Hostname</TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>www.woolf.net</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
Port number</TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>81</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
Filename</TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>/books/Orlando/chapter4.html</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
Reference</TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>p6</tt></TD>
</TR>
</TABLE>
<P>
</DIV>

<P CLASS=para>
A relative URL specifies only enough information to locate the resource 
relative to another URL. The filename component is the only part that must 
be specified for a relative URL. If the protocol, hostname, or port number 
is not specified, the value is taken from a fully specified URL. For example, 
the following is a relative URL based on the absolute URL above: 

<DIV CLASS=screen>
<P>
<PRE>
chapter6.html
</PRE>
</DIV>

<P CLASS=para>
This relative URL is equivalent to the following absolute URL: 

<DIV CLASS=screen>
<P>
<PRE>
http://www.woolf.net:81/books/Orlando/chapter6.html
</PRE>
</DIV>

<P CLASS=para>
The <tt CLASS=literal>URL</tt> class also provides 
access to the resource itself, through the <tt CLASS=literal>getContent()</tt>, 
<tt CLASS=literal>openConnection()</tt>, and <tt CLASS=literal>openStream()</tt> 
methods. However, these are all convenience functions: other classes 
do the actual work of accessing the resource. 

<P CLASS=para>
A protocol handler is an object that knows how to deal with a specific 
protocol. For example, an <tt CLASS=literal>http</tt> 
protocol handler opens a connection to an <tt CLASS=literal>http</tt> 
host. In <tt CLASS=literal>java.net</tt>, subclasses 
of <tt CLASS=literal>URLStreamHandler</tt> deal with 
different protocols. A <tt CLASS=literal>URLStreamHandlerFactory</tt> 
selects a subclass of <tt CLASS=literal>URLStreamHandler</tt> 
based on a MIME type. Once the <tt CLASS=literal>URLStreamHandler</tt> 
has established a connection with a host using a specific protocol, a subclass 
of <tt CLASS=literal>ContentHandler</tt> retrieves resource data from the host and creates an object from it. 

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.131">Class Summary</A></h2>

<DIV CLASS=screen>
<P>
<PRE>
public final class java.net.URL extends java.lang.Object
                                implements java.io.Serializable {
  // Constructors
  public URL(String spec);
  public URL(URL context, String spec);
  public URL(String protocol, String host, String file);
  public URL(String protocol, String host, int port, String file);
  // Class Methods
  public static synchronized void setURLStreamHandlerFactory(
                                  URLStreamHandlerFactory fac);
  // Instance Methods
  public boolean equals(Object obj);
  public final Object getContent();
  public String getFile();
  public String getHost();
  public int getPort();
  public String getProtocol();
  public String getRef();
  public int hashCode();
  public URLConnection openConnection();
  public final InputStream openStream();
  public boolean sameFile(URL other);
  public String toExternalForm();
  public String toString();
  // Protected Instance Methods
  protected void set(String protocol, String host, int port, 
                     String file, String ref);
}
</PRE>
</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.132">Constructors</A></h2>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.151">URL</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.171">public URL(String spec) throws MalformedURLException</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>spec</tt><br>
<DD>

<P CLASS=para>
A <tt CLASS=literal>String</tt> 
that represents a URL. </DL>
<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>MalformedURLException</tt><br>
<DD>

<P CLASS=para>
If the string is incorrectly constructed or specifies an unknown protocol. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>URL</tt> 
by parsing the given string. The string should specify an absolute URL. 
Calling this constructor is equivalent to calling <tt CLASS=literal>URL(null,</tt> 
<tt CLASS=literal>spec)</tt>. </DL>
</DIV>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.172">public URL(URL context, String spec) throws MalformedURLException</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>context</tt><br>
<DD>

<P CLASS=para>
A base <tt CLASS=literal>URL</tt> 
that provides the context for parsing <tt CLASS=literal>spec</tt>.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>spec</tt><br>
<DD>

<P CLASS=para>
A <tt CLASS=literal>String</tt> 
that represents a URL. </DL>
<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>MalformedURLException</tt><br>
<DD>

<P CLASS=para>
If the string is incorrectly constructed or specifies an unknown protocol. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>URL</tt> 
relative to the base URL specified by <tt CLASS=literal>context</tt>. 
If <tt CLASS=literal>context</tt> is not <tt CLASS=literal>null</tt>, 
and <tt CLASS=literal>spec</tt> specifies a partial 
URL, the missing parts of <tt CLASS=literal>spec</tt> 
are inherited from <tt CLASS=literal>context</tt>.

<P CLASS=para>
The given string is first parsed to see if it specifies a protocol. If 
the string contains a colon (<tt CLASS=literal>:</tt>) 
before the first occurrence of a slash (<tt CLASS=literal>/</tt>), 
the characters before the colon comprise the protocol.

<P CLASS=para>
If <tt CLASS=literal>spec</tt> does not specify a protocol, and
<tt CLASS=literal>context</tt> is not <tt CLASS=literal>null</tt>, the
protocol is inherited from <tt CLASS=literal>context</tt>, as are the
hostname, port number, and filename. If <tt CLASS=literal>context</tt> is
<tt CLASS=literal>null</tt> in this situation, the constructor throws a
<tt CLASS=literal>MalformedURLException</tt>.

<P CLASS=para>
If <tt CLASS=literal>spec</tt> does specify a protocol, and
<tt CLASS=literal>context</tt> is <tt CLASS=literal>null</tt> or specifies a
different protocol, the <tt CLASS=literal>context</tt> argument is ignored
and <tt CLASS=literal>spec</tt> should specify an absolute URL. If
<tt CLASS=literal>context</tt> specifies the same protocol as
<tt CLASS=literal>spec</tt>, the hostname, port number, and filename from
<tt CLASS=literal>context</tt> are inherited.

<P CLASS=para>
Once the constructor has created a fully specified
<tt CLASS=literal>URL</tt> object, it searches for an appropriate protocol
handler of type <tt CLASS=literal>URLStreamHandler</tt>, as described for
<tt CLASS=literal>URL(String,</tt> <tt CLASS=literal>String,</tt>
<tt CLASS=literal>int,</tt> <tt CLASS=literal>String)</tt>. Then the
<tt CLASS=literal>parseURL()</tt> method of the <tt CLASS=literal>URLStreamHandler</tt>is called to parse the remainder of the URL so that the
fields in <tt CLASS=literal>spec</tt> can override any values inherited
from <tt CLASS=literal>context</tt>.
</DL>
</DIV>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.173"><DIV CLASS=screen> <P> <PRE> public URL(String protocol, String host, String file) throws MalformedURLException </PRE> </DIV></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>protocol</tt><br>
<DD>

<P CLASS=para>
A protocol.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>host</tt><br>
<DD>

<P CLASS=para>
A hostname.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>file</tt><br>
<DD>

<P CLASS=para>
A filename. </DL>
<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>MalformedURLException</tt><br>
<DD>

<P CLASS=para>
If an unknown protocol is specified. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>URL</tt> 
with the given protocol, hostname, and filename. The port number is set 
to the default port for the given protocol. Calling this constructor is 
equivalent to calling <tt CLASS=literal>URL(protocol,</tt> 
<tt CLASS=literal>host,</tt> <tt CLASS=literal>-1,</tt> 
<tt CLASS=literal>file)</tt>. </DL>
</DIV>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.174"><DIV CLASS=screen> <P> <PRE> public URL(String protocol, String host, int port, String file) throws MalformedURLException </PRE> </DIV></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>protocol</tt><br>
<DD>

<P CLASS=para>
A protocol.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>host</tt><br>
<DD>

<P CLASS=para>
A hostname.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>port</tt><br>
<DD>

<P CLASS=para>
A port number or 
<tt CLASS=literal>-1</tt> to use the default port for the protocol.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>file</tt><br>
<DD>

<P CLASS=para>
A filename. </DL>
<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>MalformedURLException</tt><br>
<DD>

<P CLASS=para>
If an unknown protocol is specified. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>URL</tt> 
with the given protocol, hostname, port number, and filename.

<P CLASS=para>
If this is the first <tt CLASS=literal>URL</tt> object 
being created with the specified protocol, a protocol handler of type <tt CLASS=literal>URLStreamHandler</tt> 
is created for the protocol. Here are the steps that are taken to create 
a protocol handler: 

<P>
<OL CLASS=orderedlist>
<li CLASS=listitem>If an application has set up a <tt CLASS=literal>URLStreamHandlerFactory</tt>
by calling <tt CLASS=literal>setURLStreamHandlerFactory()</tt>, the
constructor calls the <tt CLASS=literal>createURLStreamHandler()</tt>
method of that object to create the protocol handler.  The protocol is passed as a 
<tt CLASS=literal>String</tt> argument to that method.

<P>
<li CLASS=listitem>If no <tt CLASS=literal>URLStreamHandlerFactory</tt> has been established, or the 
<tt CLASS=literal>createURLStreamHandler()</tt> method returns 
<tt CLASS=literal>null</tt>, the constructor retrieves the value of the 
system property <tt CLASS=literal>java.protocol.handler.pkgs</tt>.  If this
value is not <tt CLASS=literal>null</tt>, it is interpreted as a list of 
packages separated by vertical bar (<tt CLASS=literal>|</tt>) 
characters.  The constructor then tries to load the class named 
<I CLASS=emphasis>package</I><tt CLASS=literal>.</tt><I CLASS=emphasis>protocol</I><tt CLASS=literal>.Handler</tt>, 
where <I CLASS=emphasis>package</I> is the name of the first package in 
the list and <I CLASS=emphasis>protocol</I>
is the name of the protocol.  If the class exists, and is a subclass of 
<tt CLASS=literal>URLStreamHandler</tt>, it is used as the 
<tt CLASS=literal>URLStreamHandler</tt>
 for the protocol.  If the class does not exist, or if it exists 
but is not a subclass of <tt CLASS=literal>URLStreamHandler</tt>, the 
constructor tries the next package in the list.

<P>
<li CLASS=listitem>If the previous step fails to find an appropriate protocol handler, the constructor tries to load the class named 
<tt CLASS=literal>sun.net.www.protocol.</tt><I CLASS=emphasis>protocol</I><tt CLASS=literal>.Handler</tt>,
where <I CLASS=emphasis>protocol</I> is the name of the protocol.  If the 
class exists and is a subclass of <tt CLASS=literal>URLStreamHandler</tt>, it 
is used as the <tt CLASS=literal>URLStreamHandler</tt>
 for the protocol.  If the class does not exist, or if it exists 
but is not a subclass of <tt CLASS=literal>URLStreamHandler</tt>, a 
<tt CLASS=literal>MalformedURLException</tt> is thrown.

<P>
</OL>
</DL>
</DIV>

</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.133">Class Methods</A></h2>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.152">setURLStreamHandlerFactory</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.175"><DIV CLASS=screen> <P> <PRE> public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) </PRE> </DIV></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>fac</tt><br>
<DD>

<P CLASS=para>
An object that implements 
<tt CLASS=literal>URLStreamHandlerFactory</tt>. </DL>
<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>Error</tt><br>
<DD>

<P CLASS=para>
If the factory has 
already been defined.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>SecurityException</tt><br>
<DD>

<P CLASS=para>
If the application does not have permission to set the factory. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method tells the <tt CLASS=literal>URL</tt> class 
to use the given <tt CLASS=literal>URLStreamHandlerFactory</tt> 
object for handling all <tt CLASS=literal>URL</tt> 
objects. The purpose of this mechanism is to allow a program that hosts 
applets, such as a web browser, control over the creation of <tt CLASS=literal>URLStreamHandler</tt> 
objects. </DL>
</DIV>

</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.134">Instance Methods</A></h2>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.153">equals</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.176">public boolean equals(Object obj)</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>obj</tt><br>
<DD>

<P CLASS=para>
The object to be compared 
with this object. </DL>
<p>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>true</tt><br>
<DD>

<P CLASS=para>
if the objects are equivalent;

<p>
<DT CLASS=varlistentry><tt CLASS=literal>false</tt><br>
<DD>

<P CLASS=para>
if they are not. </DL>
<p>
<DT CLASS=varlistentry>Overrides<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>Object.equals()</tt> 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns <tt CLASS=literal>true</tt> if 
<tt CLASS=literal>obj</tt> is an instance of <tt CLASS=literal>URL</tt> 
with the same protocol, hostname, port number, and filename as this <tt CLASS=literal>URL</tt>. 
The reference is only compared if it is not <tt CLASS=literal>null</tt> 
in this <tt CLASS=literal>URL</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.154">getContent</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.177">public Object getContent() throws IOException</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>Object</tt> 
created from the resource represented by this <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IOException</tt><br>
<DD>

<P CLASS=para>
If any kind 
of I/O error occurs. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns the content of the <tt CLASS=literal>URL</tt>, 
encapsulated in an object that is appropriate for the type of the content. 
The method is shorthand for calling <tt CLASS=literal>openConnection().getContent()</tt>, 
which uses a <tt CLASS=literal>ContentHandler</tt> 
object to retrieve the content. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.155">getFile</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.178">public String getFile()</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The filename of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns the name of the file of this <tt CLASS=literal>URL</tt>. 
Note that the file can be misleading; although the resource represented 
by this <tt CLASS=literal>URL</tt> may be a file, 
it can also be generated on the fly by the server. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.156">getHost</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.179">public String getHost()</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The hostname of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns the hostname from this <tt CLASS=literal>URL</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.157">getPort</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.180">public int getPort()</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The port number of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns the port number of this <tt CLASS=literal>URL</tt>. 
If a port number is not specified for this <tt CLASS=literal>URL</tt>, 
meaning it uses the default port for the protocol, <tt CLASS=literal>-1</tt> is returned. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.158">getProtocol</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.181">public String getProtocol()</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The protocol of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns the protocol of this <tt CLASS=literal>URL</tt>. 
Some examples of protocols are: <tt CLASS=literal>http</tt>, 
<tt CLASS=literal>ftp</tt>, and <tt CLASS=literal>mailto</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.159">getRef</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.182">public String getRef()</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The reference of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns the reference, or anchor, of this <tt CLASS=literal>URL</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.160">hashCode</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.183">public int hashCode()</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The hashcode of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Overrides<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>Object.hashCode()</tt> 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns a hashcode for this object. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.161">openConnection</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.184">public URLConnection openConnection() throws IOException</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A <tt CLASS=literal>URLConnection</tt> object for the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IOException</tt><br>
<DD>

<P CLASS=para>
If any kind 
of I/O error occurs. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns a <tt CLASS=literal>URLConnection</tt> 
than manages a connection to the resource represented by 
this <tt CLASS=literal>URL</tt>. If there is not already 
an open connection, the method opens a connection by calling the <tt CLASS=literal>openConnection()</tt> 
method of the <tt CLASS=literal>URLStreamHandler</tt> for 
this <tt CLASS=literal>URL</tt>. A <tt CLASS=literal>URLStreamHandler</tt> 
for the protocol of the <tt CLASS=literal>URL</tt> 
is created by the constructor of the <tt CLASS=literal>URL</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.162">openStream</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.185">public final InputStream openStream() throws IOException</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A <tt CLASS=literal>InputStream</tt> that reads from this <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IOException</tt><br>
<DD>

<P CLASS=para>
If any kind 
of I/O error occurs. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns an <tt CLASS=literal>InputStream</tt> 
object that reads the content of the given <tt CLASS=literal>URL</tt>. 
The method is shorthand for calling <tt CLASS=literal>openConnection().getInputStream()</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.163">sameFile</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.186">public boolean sameFile(URL other)</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>other</tt><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>URL</tt> 
to compare. </DL>
<p>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A <tt CLASS=literal>boolean</tt> value that indicates 
if this <tt CLASS=literal>URL</tt> is equivalent to 
<tt CLASS=literal>other</tt> with the exception of 
references. 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns <tt CLASS=literal>true</tt> if 
this object and the given <tt CLASS=literal>URL</tt> 
object specify the same protocol, specify hosts that have the same IP address, 
specify the same port number, and specify the same filename. The filename 
comparison is case-sensitive. References specified by the URLs are not 
considered by this method. This method is a helper method for <tt CLASS=literal>equals()</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.164">toExternalForm</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.187">public String toExternalForm)</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A string representation of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns a string representation of this<tt CLASS=literal> URL</tt>. 
The string representation is determined by the protocol of the <tt CLASS=literal>URL</tt>. 
The method calls the <tt CLASS=literal>toExternalForm()</tt> 
method of the <tt CLASS=literal>URLStreamHandler</tt> for 
this <tt CLASS=literal>URL</tt>. A <tt CLASS=literal>URLStreamHandler</tt> 
for the protocol of the <tt CLASS=literal>URL</tt> 
is created by the constructor of the <tt CLASS=literal>URL</tt>. </DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.165">toString</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.188">public String toString()</A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A string representation of the <tt CLASS=literal>URL</tt>. 

<p>
<DT CLASS=varlistentry>Overrides<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>Object.toString()</tt> 

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method returns a string representation of this<tt CLASS=literal> URL</tt> 
by calling <tt CLASS=literal>toExternalForm()</tt>. </DL>
</DIV>

</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.135">Protected Instance Methods</A></h2>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch15-REFSECT2-AUTOID.166">set</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch15-REFSECT3-AUTOID.189"><DIV CLASS=screen> <P> <PRE> protected void set(String protocol, String host, int port, String file, String ref) </PRE> </DIV></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>protocol</tt><br>
<DD>

<P CLASS=para>
A protocol.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>host</tt><br>
<DD>

<P CLASS=para>
A hostname.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>port</tt><br>
<DD>

<P CLASS=para>
A port number.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>file</tt><br>
<DD>

<P CLASS=para>
A filename.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>ref</tt><br>
<DD>

<P CLASS=para>
A reference. </DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
This method sets the protocol, hostname, port number, filename, and reference 
of this <tt CLASS=literal>URL</tt>. The method is 
called by a <tt CLASS=literal>URLStreamHandler</tt> 
to set the parts of the <tt CLASS=literal>URL</tt>. 
A <tt CLASS=literal>URLStreamHandler</tt> for the 
protocol of the <tt CLASS=literal>URL</tt> is created 
by the constructor of the <tt CLASS=literal>URL</tt>. 
It is this <tt CLASS=literal>URLStreamHandler</tt> 
that parses the URL string. This method is used after parsing to set the 
values of the <tt CLASS=literal>URL</tt>. </DL>
</DIV>

</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.136">Inherited Methods</A></h2>

<DIV CLASS=informaltable>
<P>
<TABLE CLASS=INFORMALTABLE>
<TR CLASS=row>
<TH ALIGN="left">Method</TH>
<TH ALIGN="left">Inherited From</TH>
<TH ALIGN="left">Method</TH>
<TH ALIGN="left">Inherited From</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>clone()</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>finalize()</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>getClass()</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>notify()</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>notifyAll()</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>wait()</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>wait(long)</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>wait(long, int)</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>Object</tt></TD>
</TR>
</TABLE>
<P>
</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch15-REFSECT1-AUTOID.137">See Also</A></h2>

<P CLASS=para>
<tt CLASS=literal>ContentHandler</tt>, 
<tt CLASS=literal>Error</tt>, 
<tt CLASS=literal>InputStream</tt>, 
<tt CLASS=literal>IOException</tt>, 
<tt CLASS=literal>MalformedURLException</tt>, 
<tt CLASS=literal>SecurityException</tt>, 
<tt CLASS=literal>URLConnection</tt>, 
<tt CLASS=literal>URLStreamHandler</tt>, 
<tt CLASS=literal>URLStreamHandlerFactory</tt> 
</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch15_19.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch15_21.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>SocketImplFactory</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>URLConnection</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
